package com.fishery.processing.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.entity.Result;
import com.fishery.processing.entity.Craft;
import com.fishery.processing.entity.Factory;
import com.fishery.processing.entity.vo.CraftVo;
import com.fishery.processing.entity.vo.FactoryVo;
import com.fishery.processing.service.CraftService;
import com.fishery.processing.service.FactoryService;
import com.fishery.processing.util.ConvertUtil;
import com.fishery.processing.util.UpdateProxy;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-01-14
 */
@Api(tags = "工厂管理相关接口")
@CrossOrigin
@RestController
@RequestMapping("/processingFactory/factory")
public class FactoryController {

    @Autowired
    private FactoryService factoryService;

    @Autowired
    private UpdateProxy updateProxy;

    @Autowired
    private ConvertUtil convertUtil;

    @PostMapping("/")
    @ApiOperation("创建加工厂")
    public Result createFactory(@Valid @RequestBody @ApiParam(required = true,value = "factoryVo") FactoryVo factoryVo){
        return factoryService.createFactory(convertUtil.convert(factoryVo,Factory.class)) == 1
                ? Result.success("创建成功") : Result.error("创建失败");
    }

    @PutMapping("/")
    @ApiOperation("修改加工厂信息")
    public Result updateFactory(@Valid @RequestBody @ApiParam(required = true,value = "factoryVo") FactoryVo factoryVo){
        Factory factory = factoryService.searchFactoryById(factoryVo.getId());
        BeanUtils.copyProperties(factoryVo,factory);
        //使用反射对修改操作进行检查
        return ((FactoryService)(updateProxy.bind(factoryService).getProxyInstance()))
                .updateFactory(factory) == 1
                ? Result.success("修改成功") : Result.error("修改失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除加工厂")
    public Result deleteFactory(@PathVariable(value = "id") String factoryId){
        return factoryService.deleteFactory(factoryId) == 1 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据工厂id获取加工厂信息")
    public Result searchFactoryById(@PathVariable(value = "id") String factoryId){
        return Result.success()
                .data(convertUtil.convert(factoryService.searchFactoryById(factoryId),FactoryVo.class));
    }

    @GetMapping("/baseFactory/{id}/{page}/{size}")
    @ApiOperation("分页根据基地id获取加工厂列表")
    public Result searchFactoriesOfBase(@PathVariable(value = "id") String baseId
                                        ,@PathVariable(value = "page") Integer page
                                        ,@PathVariable(value = "size") Integer size){
        return Result.success()
                .data(convertUtil.convert(factoryService.searchFactoriesOfBase(baseId, page, size),FactoryVo.class));
    }

    @GetMapping("/baseFactory/{id}")
    @ApiOperation("根据基地id获取加工厂列表")
    public Result searchFactoriesOfBase(@PathVariable(value = "id") String baseId){
        return Result.success()
                .data(convertUtil.convert(factoryService.searchFactoriesOfBase(baseId),FactoryVo.class));
    }

    @GetMapping("/typeFactory/{type}")
    @ApiOperation("根据加工品类名称获取全部加工厂")
    public Result searchFactoryByType(@PathVariable(value = "type") String type){
        return Result.success()
                .data(convertUtil.convert(factoryService.searchFactoryByType(type),FactoryVo.class));
    }

    @GetMapping("/typeFactory/{type}/{page}/{size}")
    @ApiOperation("根据加工品类名称分页获取加工厂")
    public Result searchFactoryByType(@PathVariable(value = "type") String type
                                        ,@PathVariable(value = "page") Integer page
                                        ,@PathVariable(value = "size") Integer size){
        return Result.success()
                .data(convertUtil.convert(factoryService.searchFactoryByType(type,page,size),FactoryVo.class));
    }

    @GetMapping("/factoryMessageOfBase/{id}")
    @ApiOperation("根据基地id获取加工厂的总数量和总面积")
    public Result searchFactoryMessageOfBase(@PathVariable(value = "id") String baseId){
        return Result.success().data(factoryService.searchFactoryMessageOfBase(baseId));
    }

    @GetMapping("/factoryMessageOfAll")
    @ApiOperation("获取所有加工厂的id、名称、地址、基地编号等信息")
    public Result searchFactoryMessageOfAll(){
        return Result.success().data(factoryService.searchFactoryMessageOfAll());
    }

}

